function [myStateMachine, myTaskList] = configureMyTask
% Configure objects and functions for a demonstration task.
%
% configureMyTask.m goes with howToMakeATask.m, as an example of a simple
% Snow Dots task.  Note that howToMakeATask.m is not intended to be read
% directly. It contains the "source comments" for a page in the Snow Dots
% documentation called "How to Make a New Snow Dots Task".

%% The Task List

% a list with a few items of layout data
myTaskList = topsGroupedList;
myTaskList{'layout'}{'left side'} = -5;
myTaskList{'layout'}{'right side'} = 5;
myTaskList{'layout'}{'dot pixelSize'} = 20;
myTaskList{'layout'}{'light color'} = [200 100 0];
myTaskList{'layout'}{'dark color'} = [25 50 50];

% add some reusable "ideas"
ready = myTaskList.addIdea('ready');
left = myTaskList.addIdea('left');
right = myTaskList.addIdea('right');
done = myTaskList.addIdea('done');

%% Objects That Do Stuff

% create graphics objects
dm = dotsTheDrawablesManager.theObject;
leftTarget = dm.newObject('dotsDrawableTargets');
rightTarget = dm.newObject('dotsDrawableTargets');
myTaskList{'graphics objects'}{'left target'} = leftTarget;
myTaskList{'graphics objects'}{'right target'} = rightTarget;

% set up the targets at different sides of the visual scene
leftTarget.x = myTaskList{'layout'}{'left side'};
leftTarget.pixelSize = myTaskList{'layout'}{'dot pixelSize'};
rightTarget.x = myTaskList{'layout'}{'right side'};
rightTarget.pixelSize = myTaskList{'layout'}{'dot pixelSize'};

% create a keyboard input object
kb = dotsReadableHIDKeyboard();
myTaskList{'input objects'}{'keyboard'} = kb;

% set the keyboard object to return "ideas" as outputs
leftInput = kb.phenomenons{'pressed'}{'pressed_KeyboardF'};
rightInput = kb.phenomenons{'pressed'}{'pressed_KeyboardJ'};
doneInput = kb.phenomenons{'pressed'}{'pressed_KeyboardSpacebar'};
kb.addClassificationInGroupWithRank(leftInput,	left,	ready,	1);
kb.addClassificationInGroupWithRank(rightInput, right,	ready,	2);
kb.addClassificationInGroupWithRank(doneInput,  done,	ready,	3);
kb.activeClassificationGroup = ready;

%% Objects That Coordinate Objects That Do Stuff

% create a state machine object to coordinate the graphics and input
myStateMachine = topsStateMachine;
myStateMachine.name = 'my state machine';
myTaskList{'coordinating objects'}{'state machine'} = myStateMachine;

% define a few states using "ideas" as state names
myStates = { ...
    'name', 'next', 'entry'                             'input'; ...
    ready,  ready,  {@readData, kb},                    {@queryNextTracked, kb}; ...
    left,   ready,	{@lightUp, leftTarget, myTaskList}, {}; ...
    right,  ready,	{@lightUp, rightTarget, myTaskList},{}; ...
    done,   '',     {},                                 {}; ...
    };
myStateMachine.addMultipleStates(myStates);

% connect the state machine object to some "glue" functions
%   which are defined below
myStateMachine.startFevalable = {@beginMyTask, myTaskList};
myStateMachine.finishFevalable = {@endMyTask, myTaskList};

%% Glue Functions

% how to start the task
function beginMyTask(myTaskList)
leftTarget = myTaskList{'graphics objects'}{'left target'};
rightTarget = myTaskList{'graphics objects'}{'right target'};
leftTarget.colors = myTaskList{'layout'}{'dark color'};
rightTarget.colors = myTaskList{'layout'}{'dark color'};

dm = dotsTheDrawablesManager.theObject;
dm.activateGroup(dm.defaultGroup);
dm.openScreenWindow;
dm.mayDrawNextFrame;

% how to finish the task
function endMyTask(myTaskList)
dm = dotsTheDrawablesManager.theObject;
dm.closeScreenWindow;

% how to light up one of the targets
function lightUp(whichTarget, myTaskList)
whichTarget.colors = myTaskList{'layout'}{'light color'};
dm = dotsTheDrawablesManager.theObject;
dm.mayDrawNextFrame;